﻿using System;
using System.IO;
using System.Text;
using System.Windows.Forms;

namespace Program13
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void btnOpen_Click(object sender, EventArgs e)
        {
            //点击弹出对话框
            var dialog = new OpenFileDialog();

            //设置对话框属性
            dialog.Title = "请选择要打开的文本文件";//标题
            dialog.Multiselect = true;//支持对话框多选
            dialog.InitialDirectory = @"c\AAA";//打开默认目录
            dialog.Filter = "文本文件|*.txt";//设置另存为文件类型和打开文件类型提示框显示的选择内容

            //展开对话框
            dialog.ShowDialog();

            //获取选中文件的路径
            var paths = dialog.FileNames;
            if (paths.Length == 0) {
                MessageBox.Show("请选择一个文件！");
                return;
            //报错
            }

            //读取二进制文件，显示在文本框
            textBox1.Text = string.Empty;
            foreach(var path in paths)
            {
                string str;
                using (FileStream fsRead = new FileStream(path,FileMode.OpenOrCreate,FileAccess.Read))
                {
                    //缓冲区
                    byte[] buffer = new byte[1024 * 1024 * 5];
                    int r = fsRead.Read(buffer, 0, buffer.Length);

                    //将读出的二进制文件转换成utf-8格式的字符串
                    str = Encoding.UTF8.GetString(buffer, 0, r)+"\r";
                }
                textBox1.Text += str;
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            //通过对话框选择保存的路径
            //点击弹出对话框
            var dialog = new SaveFileDialog();

            //设置对话框属性
            dialog.Title = "请选择要打开的文本文件";//标题
            dialog.InitialDirectory = @"c\AAA";//打开默认目录
            dialog.Filter = "文本文件|*.txt";//设置另存为文件类型和打开文件类型提示框显示的选择内容

            //展开对话框
            dialog.ShowDialog();

            //二进制文件的写入
            var path = dialog.FileName;//文件名字符串(文件地址)
            if (path.Length == 0)
            {
                MessageBox.Show("请选择一个要保存的文件！");
                return;
                //报错
            }

            using (FileStream fsWrite  = new FileStream(path,FileMode.OpenOrCreate,FileAccess.Write))
            {
                byte[] buffer = Encoding.UTF8.GetBytes(textBox1.Text);
                fsWrite.Write(buffer, 0, buffer.Length);
            }
            MessageBox.Show("保存成功！");
        }

        private void button2_Click(object sender, EventArgs e)
        {
            //选择字体
            var fd = new FontDialog();
            //弹出对话框
            fd.ShowDialog();

            //把选好的字体赋值给文本框
            textBox1.Font = fd.Font;
        }

        private void button3_Click(object sender, EventArgs e)
        {
            //选择颜色
            var fd = new ColorDialog();
            //弹出对话框
            fd.ShowDialog();

            //把选好的字体赋值给文本框
            //前景色
            textBox1.ForeColor = fd.Color;
            //背景色 textBox.BackColor
        }
    }
}
